<template>
  <div>
    <el-row :gutter="15">
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="100px"
      >
        <el-col :span="24">
          <el-form-item label="手机号" prop="mobile">
            <el-input
              v-model="formData.mobile"
              placeholder="请输入手机号"
              :maxlength="11"
              show-word-limit
              clearable
              prefix-icon="el-icon-mobile"
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="单行文本" prop="field101">
            <el-input
              v-model="formData.field101"
              placeholder="请输入单行文本"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="计数器" prop="field102">
            <el-input-number
              v-model="formData.field102"
              placeholder="计数器"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="上传" prop="field103" required>
            <el-upload
              ref="field103"
              :file-list="field103fileList"
              :action="field103Action"
              :before-upload="field103BeforeUpload"
            >
              <el-button size="small" type="primary" icon="el-icon-upload"
                >点击上传</el-button
              >
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="评分" prop="field104">
            <el-rate v-model="formData.field104"></el-rate>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="日期选择" prop="field105">
            <el-date-picker
              v-model="formData.field105"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              :style="{ width: '100%' }"
              placeholder="请选择日期选择"
              clearable
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        mobile: '',
        field101: undefined,
        field102: undefined,
        field103: null,
        field104: 0,
        field105: null,
      },
      rules: {
        mobile: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'blur',
          },
          {
            pattern: /^1(3|4|5|7|8|9)\d{9}$/,
            message: '手机号格式错误',
            trigger: 'blur',
          },
        ],
        field101: [
          {
            required: true,
            message: '请输入单行文本',
            trigger: 'blur',
          },
        ],
        field102: [
          {
            required: true,
            message: '计数器',
            trigger: 'blur',
          },
        ],
        field104: [
          {
            required: true,
            message: '评分不能为空',
            trigger: 'change',
          },
        ],
        field105: [
          {
            required: true,
            message: '请选择日期选择',
            trigger: 'change',
          },
        ],
      },
      field103Action: 'https://jsonplaceholder.typicode.com/posts/',
      field103fileList: [],
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['elForm'].validate((valid) => {
        if (!valid) return
        // TODO 提交表单
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
    field103BeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      return isRightSize
    },
  },
}
</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

.el-rate {
  display: inline-block;
  vertical-align: text-top;
}
</style>
